
package ClientLogic

import (
"CRM_rorm/logic/StaffLogic"
"CRM_rorm/model"
"CRM_rorm/response"
"CRM_rorm/service"
"github.com/gin-gonic/gin"
)



func ClientAdd(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var client model.Client
	err := ctx.Bind(&client)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ClientProjectModel.Insert(client)
	if err != nil {
		response.Fail(ctx, nil, "增加失败")
		return
	}
	response.Success(ctx, nil, "增加成功")
}

func ClientDeleteSoft(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var client model.Client
	err := ctx.Bind(&client)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ClientProjectModel.DeleteSoft(client)
	if err != nil {
		response.Fail(ctx, nil, "软删除失败")
		return
	}
	response.Success(ctx, nil, "软删除成功")
}

func ClientDelete(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var client model.Client
	err := ctx.Bind(&client)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ClientProjectModel.Delete(client)
	if err != nil {
		response.Fail(ctx, nil, "删除失败")
		return
	}
	response.Success(ctx, nil, "删除成功")
}

func ClientFindByClientId(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var client model.Client
	err := ctx.Bind(&client)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	client, err = service.Svc.ClientProjectModel.FindOneByClientId(client)
	if err != nil {
		response.Fail(ctx, nil, "查找失败")
		return
	}
	response.Success(ctx, gin.H{
		"client_id":   client.Client_id,
		"client_name": client.Client_name,
		"client_project_id" :client.Client_Project_id,
		"client_telephone" : client.Client_Telephone,
	}, "查询成功")

}

func ClientUpdate(ctx *gin.Context)  {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var client model.Client
	err := ctx.Bind(&client)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	err = service.Svc.ClientProjectModel.Update(client)
	if err != nil {
		response.Fail(ctx,nil,"更新失败")
		return
	}
	response.Success(ctx,nil,"更新成功")
}

func ClientFindByProjectId(ctx *gin.Context) {
	if StaffLogic.Role != "admin" {
		response.Fail(ctx, nil, "权限不足")
		return
	}
	var client model.Client
	err := ctx.Bind(&client)
	if err != nil {
		response.Fail(ctx, nil, "绑定失败")
		return
	}
	client, err = service.Svc.ClientProjectModel.FindOneByProjectId(client)
	if err != nil {
		response.Fail(ctx, nil, "查找失败")
		return
	}
	response.Success(ctx, gin.H{
		"client_id":   client.Client_id,
		"client_name": client.Client_name,
		"client_project_id" :client.Client_Project_id,
		"client_telephone" : client.Client_Telephone,
	}, "查询成功")

}